package com.admin.business.voteReconsideration;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 首次发起提交申请批复记录表
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteReconsideration")
@Slf4j
public class VoteReconsiderationService {

    @Resource
    VoteReconsiderationDao voteReconsiderationDao;

    /**
     * 添加首次发起提交申请批复记录表,获取id数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - essential_information_id:基本信息id
     *                   - template_img:成立业主委员会申请表模板
     *                   - template_imgs:成立业主委员会申请表
     *                   - certificates_img:上传街道办/房屋管理局执或批发复函
     *                   - audit_status:审核状态：0、审核中，1、审核成功，2、审核失败；
     *                   - submit_time:递交时间
     *                   - reply_time:批复时间
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return addVoteReconsideration(CDO cdoRequest) {

        CDO voteRecons = new CDO();
        voteRecons.set("essential_information_id", cdoRequest.getString("essential_information_id"));
        int voteReconsiderationCount = voteReconsiderationDao.getVoteReconsiderationCount(voteRecons);
        if (voteReconsiderationCount == 0) {
            voteReconsiderationDao.addVoteReconsideration(cdoRequest);
        } else {
            CDO voteReconsideration = voteReconsiderationDao.getVoteReconsideration(voteRecons);
            if (voteReconsideration != null) {
                cdoRequest.set("id", voteReconsideration.getString("id"));
                voteReconsiderationDao.updateVoteReconsiderationById(cdoRequest);
            }
        }


        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新首次发起提交申请批复记录表数据
     *
     * @param cdoRequest - id:
     *                   - user_id:用户id
     *                   - essential_information_id:基本信息id
     *                   - template_img:成立业主委员会申请表模板
     *                   - template_imgs:成立业主委员会申请表
     *                   - certificates_img:上传街道办/房屋管理局执或批发复函
     *                   - audit_status:审核状态：0、审核中，1、审核成功，2、审核失败；
     *                   - submit_time:递交时间
     *                   - reply_time:批复时间
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteReconsiderationById(CDO cdoRequest) {
        int affectedRows = voteReconsiderationDao.updateVoteReconsiderationById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除首次发起提交申请批复记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteReconsiderationById(CDO cdoRequest) {
        int affectedRows = voteReconsiderationDao.deleteVoteReconsiderationById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除首次发起提交申请批复记录表数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteReconsideration(CDO cdoRequest) {
        int affectedRows = voteReconsiderationDao.deleteVoteReconsideration(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取首次发起提交申请批复记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteReconsiderationById(CDO cdoRequest) {
        CDO cdoMapperResult = voteReconsiderationDao.getVoteReconsiderationById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取首次发起提交申请批复记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteReconsideration(CDO cdoRequest) {
        CDO cdoMapperResult = voteReconsiderationDao.getVoteReconsideration(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取首次发起提交申请批复记录表数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteReconsiderationList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteReconsiderationDao.getVoteReconsiderationList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteReconsiderationDao.getVoteReconsiderationListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取首次发起提交申请批复记录表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteReconsiderationListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteReconsiderationDao.getVoteReconsiderationListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteReconsiderationDao.getVoteReconsiderationListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
